#include "localhostinfo.h"

#include <QNetworkInterface>
#include <QNetworkAddressEntry>
#include <QProcess>
#include <QRegularExpression>
#include <QFile>
#include "dcuinformation.h"

LocalHostInfo::LocalHostInfo(QWidget *parent) : QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);
    setAutoFillBackground(true);
    QPalette background = palette();
    background.setBrush(QPalette::Window, QBrush(QPixmap(":/images/defaultUI.png")));
    setPalette(background);

    initLeftLabel();
    initRightLabel();
    setNetInfo();
    QPushButton *btn = new QPushButton(this);
    btn->setText("不启用");
    btn->setFont(QFont(ZITI, 18, QFont::Normal));
    btn->setGeometry(QRect(300, 330, 90, 40));

    QPushButton *backBtn = new QPushButton(this);
    backBtn->setText("返回");
    backBtn->setGeometry(QRect(620, 350, 150, 50));
    backBtn->setFont(QFont(ZITI, 18, QFont::Normal));
    connect(backBtn, SIGNAL(clicked()), this, SLOT(backBtnDone()));
}

void LocalHostInfo::initLeftLabel()
{
    int         total = 5;
    QStringList textList;
    textList << "登陆密码"
             << "IP 地址"
             << "默认网关"
             << "子网掩码"
             << "故障告警";
    for (int var = 0; var < total; ++var)
    {
        QLabel *label = new QLabel(this);
        label->setText(textList.at(var));
        label->setFont(QFont(ZITI, 18, QFont::Normal));
        label->setGeometry(QRect(190, 50 + (35 + 35) * var, 100, 35));
        label->setStyleSheet("color:white;");
    }
}

void LocalHostInfo::initRightLabel()
{
    int total = 4;

    for (int var = 0; var < total; ++var)
    {
        QLabel *label = new QLabel(this);
        label->setText("192.168.100.200");
        label->setFont(QFont(ZITI, 18, QFont::Normal));
        label->setGeometry(QRect(300, 50 + (35 + 35) * var, 200, 35));
        label->setStyleSheet("color:black;background:white");
        labelList.append(label);
    }
}

void LocalHostInfo::getNetInfo(QString &ip, QString &gateway, QString &mask)
{
    QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();

    QNetworkInterface ret;

    foreach (QNetworkInterface interface, interfaces)
    {
        if (interface.name() == "eth0")
        {
            ret = interface;
            break;
        }
    }

    QList<QNetworkAddressEntry> addressEntries = ret.addressEntries();
    foreach (QNetworkAddressEntry entry, addressEntries)
    {
        if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol)
        {
            if (!entry.ip().toString().contains("169.254"))
            {
                ip   = entry.ip().toString();
                mask = entry.netmask().toString();
            }
        }
    }

    QProcess process;
    process.start("ip route");
    process.waitForFinished();
    QByteArray part = process.readAll().split('\n').at(1);
    if (part.contains("default via"))
    {
        QRegularExpression      ipRegex("\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b");
        QRegularExpressionMatch match = ipRegex.match(part);
        if (match.hasMatch())
        {
            gateway = match.captured(0);
        }
        else
        {
            gateway = readIpConfig();
        }
    }
    else
    {
        gateway = readIpConfig();
    }
}

void LocalHostInfo::setNetInfo()
{
    getNetInfo(m_ip, m_gateway, m_mask);
    labelList.at(0)->setText("1");
    labelList.at(1)->setText(m_ip);
    labelList.at(2)->setText(m_gateway);
    labelList.at(3)->setText(m_mask);
}

void LocalHostInfo::backBtnDone()
{
    emit pageInfo("返回");
}

QString LocalHostInfo::getIp()
{
    return m_ip;
}

QString LocalHostInfo::readIpConfig()
{
    QString path     = "/opt/ipconfig.sh";       //查找的文件
    QString find_str = "route add default gw ";  // 查找的前方字符串
    QFile   f(path);

    QString back_value = 0;

    if (f.open(QIODevice::ReadOnly | QIODevice::Text))  // 只读
    {
        qDebug() << "找到了相应设置IP的文件";
        QTextStream in(&f);  //文件流读取全部文件内容
        QString     qtstr = in.readAll();

        int bpos = qtstr.indexOf(find_str);  //从前面开始查找，找到相应字符串的位置

        int find_str_length = find_str.length();  // 读取字符串长度

        back_value = qtstr.mid(bpos + find_str_length, 15);

        QRegularExpression      ipRegex("\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b");
        QRegularExpressionMatch match = ipRegex.match(back_value);
        if (match.hasMatch())
        {
            back_value = match.captured(0);
        }
        else
        {
            back_value = "";
        }

        qDebug() << "配置文件中返回的值是" << back_value;

        f.close();  //关闭文件
    }
    else
    {
        qDebug() << "并未发现ipconfig.sh";
    }
    return back_value;  // 0 无文件
}
